{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

CS206: Data Structures

\n", "

Douglas Blank
Bryn Mawr College

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 1. Introduction to Java" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The goal of this course is to explore the *structures of computation*. These include:\n", "\n", "* Data structures\n", "* Object-Oriented Programming\n", "* Problem solving\n", "* Processes\n", "\n", "If you took the Introduction to Computer Science at Bryn Mawr College or Haverford College, then you have learned some Processing or Python, respectively. We will be transitioning from those languages and systems to a new one based on Java9. To that, we will use many different tools, starting with Jupyter." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.1 Jupyter" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Jupyter is a set of new technologies:\n", "\n", "* Web-based computational tools; nothing to download or install\n", "* Major focus on the \"notebook\" (one of which you are looking at)\n", " * This is a Java9 notebook\n", " * Composed of cells\n", " * Blends writing, graphics, and computation together\n", " * Languages (called \"kernels\") can be the focus of a notebook\n", " * Code in other languages can be run through a magic cell\n", "* Share code with others by \"publishing\" notebooks\n", "\n", "Here is an example of how you will use the notebook with computation:" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, world!\n", "\n" ] } ], "source": [ "System.out.println(\"Hello, world!\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.2 Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Some of you may have learned a bit about Python:\n", "\n", "* Python has implicit types\n", "* Whitespace is meaningful (indentation is part of language)\n", "* Almost everything is an \"object\"\n", "* Comes with a large library of utility functions (modules)\n", "\n", "Here is an example of Python in a Java9 notebook via a %% magic:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, world!\n", "170\n" ] } ], "source": [ "%%python\n", "\n", "# Here is a function definition:\n", "def multiply(a, b):\n", " return a * b\n", "\n", "# And some output:\n", "print(\"Hello, world!\")\n", "print(multiply(34, 5)) # Calling a function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.3 Processing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Simplified Java\n", "* Designed for artistic creations\n", "* Uses curly braces rather than indentation for the computer; indentation is useful for the human\n", "* Process is pre-defined around `setup()` and `draw()` functions\n", "\n", "Here is an example of Processing in a Java9 notebook via a %% magic:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%processing\n", "\n", "// Here is a function definition:\n", "int multiply(int a, int b) {\n", " return a * b;\n", "}\n", "\n", "void setup() {\n", " fill(0);\n", "}\n", "\n", "void draw() {\n", " text(\"Hello, world!\", 10, 20);\n", " text(str(multiply(34, 5)), 10, 35); // Calling a function\n", " noLoop();\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is better support for both Python and Processing via their own kernels rather than these magics.\n", "\n", "# 2. Java\n", "\n", "Java is typically a \"compiled\" language. This means that you generally can't do anything unless you compile code into something that can be run.\n" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Created file '/home/dblank/public_html/CS206 Data Structures/2016-Spring/Notebooks/TestClass.java'.\n" ] } ], "source": [ "%%file TestClass.java\n", "\n", "class TestClass {\n", "\n", " public static void main(String [] args) {\n", " System.out.println(\"Hello, world!\");\n", " }\n", "\n", "}" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, world!\r\n", "\n" ] } ], "source": [ "%%shell\n", "\n", "javac TestClass.java\n", "java TestClass\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But in this course, we will be using an interpreter rather than a compiler. Sometimes this is called a REPL which stands for Read-Eval-Print-Loop. Basically, you can type \"snippets\" of code interactively, one cell at a time." ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, world!\n", "\n" ] } ], "source": [ "System.out.println(\"Hello, world!\");" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Added variable my_greeting of type String with initial value \"Hello, Doug!\"\n", "\n" ] } ], "source": [ "String my_greeting = \"Hello, Doug!\";" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, Doug!\n", "\n" ] } ], "source": [ "System.out.println(my_greeting);" ] } ], "metadata": { "kernelspec": { "display_name": "Java 9", "language": "java", "name": "java9" }, "language_info": { "file_extension": ".class", "mimetype": "application/java-vm", "name": "java" } }, "nbformat": 4, "nbformat_minor": 0 }